package com.d3works.apps.account.web;

import com.d3works.apps.account.db.AccountConfig;
import com.d3works.apps.account.db.AnalyticAxisByCompany;
import com.d3works.apps.account.db.repo.AccountConfigRepository;
import com.d3works.apps.account.service.analytic.AccountConfigAnalyticService;
import com.d3works.apps.account.service.move.SimulatedMoveService;
import com.d3works.exception.ResponseMessageType;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;
import java.util.List;

@Singleton
public class AccountConfigController {

  public void deactivateSimulatedMoves(ActionRequest request, ActionResponse response) {

    AccountConfig accountConfig = request.getContext().asType(AccountConfig.class);
    try {
      Beans.get(SimulatedMoveService.class).deactivateSimulatedMoves(accountConfig.getCompany());
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  public void checkChangesInAnalytic(ActionRequest request, ActionResponse response) {
    try {
      AccountConfig accountConfig = request.getContext().asType(AccountConfig.class);
      List<AnalyticAxisByCompany> initialList =
          Beans.get(AccountConfigRepository.class)
              .find(accountConfig.getId())
              .getAnalyticAxisByCompanyList();
      List<AnalyticAxisByCompany> modifiedList = accountConfig.getAnalyticAxisByCompanyList();
      Beans.get(AccountConfigAnalyticService.class)
          .checkChangesInAnalytic(initialList, modifiedList);
    } catch (Exception e) {
      TraceBackService.trace(response, e, ResponseMessageType.WARNING);
    }
  }
}
